<?php
class Users_db extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

	function getUser($data)
	{
		$this->db->where($data); 
		return $this->db->get('users')->row_array();		
	}
    
    function get_roles() {
        $query = $this->db->get('roles');
        $result = $query->result_array();
        return $result;
    }
    
    function is_username_unique($username, $pk=0) {
        if($pk!=0)
        $this->db->where('userid !=',$pk);
        //print_object($title); exit;
        $where = "(username='".$username."')"; 
        $this->db->where($where);
        $result = $this->db->count_all_results('users');
        //print_object($this->db->last_query()); exit;
        if($result>0)
            return false;
        else
            return true; 
    }
    
    function is_old_password( $userid, $password ) {
        $where = array('userid'=>$userid, 'password'=>md5($password)); 
        $this->db->where($where);
        $result = $this->db->count_all_results('users');
        //print_object($this->db->last_query()); exit;
        if($result==0)
            return false;
        else
            return true;   
    } 
	
	function save_user($data,$pk='') {
        $save_result = array();
        
        if ($pk=='') {
    		$this->db->insert('users', $data);
    		$save_result['insert_id'] = $this->db->insert_id();
            if($save_result['insert_id'] == 0 || $save_result['insert_id'] == false) {
                $save_result['result'] = 'F';
                $save_result['error_no'] = $this->db->_error_number();
                $save_result['error_message'] = $this->db->_error_message();
            } else {
                $save_result['result'] = 'S';
            }
		} else {
    		$this->db->where('userid', $pk);
    		$this->db->update('users', $data);
            //print_object($this->db->last_query());exit;
            $save_result['affected_rows'] = $this->db->affected_rows();
            if($save_result['affected_rows'] == -1 ) {
                $save_result['result'] = 'F';
                $save_result['error_no'] = $this->db->_error_number();
                $save_result['error_message'] = $this->db->_error_message();
            } else {
                $save_result['result'] = 'S';
            }
		}   
        return $save_result;
    }
    
       
    function get_user_list($limit=0,$start=0,$where='') {
        $this->db->select('userid ,  username ,  password ,  usr.roleid , rol.roletitle, disabled ,  name ,  mobile ,  email ,  address ,  firstlogin ,  createdby, createdon');
        $this->db->from('users usr');
        $this->db->join('roles rol','usr.roleid=rol.roleid','left');
        if($where!='')
            $this->db->where($where);
        if($limit!=0)
            $this->db->limit($limit,$start); 
        $query = $this->db->get();
        $result = $query->result_array();
        return $result; 
    }
    
    function get_role_list($is_grid_data = false) {
        $query = $this->db->get('roles');
        $result = $query->result_array();
        if($is_grid_data) {
            $result = format_list($result, "roleid", "roletitle");
        }
        return $result;
    }
    
    function get_subnav($titleurl='') {
        $subnav = array();
        $faclist = $this->get_user(array('published'=>1));
        foreach($faclist as $fac) {
            $nav = array();
            $nav['title'] = $fac['factitle'];
            $nav['sdesc'] = $fac['facsdesc'];
            $nav['url'] = "user/".$fac['facurl'];
            if($titleurl!='') {
                if(strcasecmp($titleurl,$fac['facurl']) == 0) {
                    $nav['selected'] = true;    
                }
            }
            array_push($subnav,$nav);    
        }
        return $subnav;
    }
    
    function get_user($data) {
        $this->db->from('users usr');
        $this->db->join('roles rls','usr.roleid = rls.roleid','left');
        $this->db->where($data);
        $query = $this->db->get();
        $result = $query->result_array();
        return $result; 
    }
    
    function build_user_dg_data($fiterdata='') {
        $result['dg_title'] = "User List";
        $result['current_page'] = ($this->input->post('page')) ? $this->input->post('page') : 1;
        $result['rows_per_page'] = ($this->config->item('per_page')) ? $this->config->item('per_page') : 10;
        if($fiterdata=='')
            $result['total'] = $this->get_user_filter_count();
        else
            $result['total'] = $this->get_user_filter_count($fiterdata);
        $result['pk'] = 'userid';
        $result['itemname'] = 'username';
        $result['dgcolumns'] = array(
            'username'=>array('label'=>'User Name','type'=>'link','visible'=>true, 'href'=>'/admin/viewuser'),
            'roletitle'=>array('label'=>'Role','type'=>'string','visible'=>true),
            'disabled'=>array('label'=>'Disabled','type'=>'boolean','visible'=>true),
            'name'=>array('label'=>'Name','type'=>'string','visible'=>true), 
            'mobile'=>array('label'=>'Mobile','type'=>'string','visible'=>true), 
            'email'=>array('label'=>'Email','type'=>'string','visible'=>true), 
            'address'=>array('label'=>'Address','type'=>'string','visible'=>true)          
        );
        
        $result['actions'] = array(
        'add'=>'/admin/adduser',
        'edit'=>'/admin/edituser',
        'delete'=>'/admin/deleteuser',
        'view'=>'/admin/viewuser',
        'page'=>'/admin/viewusers'
        );
        
        $offset = $result['rows_per_page']*($result['current_page']-1);
        $result['pages_total'] =  ceil($result['total']/$result['rows_per_page']);
        if($fiterdata=='')
            $result["dgitems"] = $this->get_user_filter_list($result['rows_per_page'], $offset, '');
        else
            $result["dgitems"] = $this->get_user_filter_list($result['rows_per_page'], $offset, $fiterdata);
        return $result;
    }
    
    function get_user_filter_count($filterdata='') {
        $this->db->from('users usr');
        $this->db->join('roles rls','usr.roleid = rls.roleid','left');
        
        if($filterdata!='') {
            $fitems = $filterdata['items'];
            foreach($fitems as $fitem) {
                $tbl = $fitem['table'];
                $field = $fitem['name'];
                $value = $fitem['value'];
                if( $value != '') {
                    if(is_array($value)) {
                        $this->db->where_in($tbl.'.'.$field,$value);
                    } else {
                        if($fitem['type']=='text')
                            $this->db->like($tbl.'.'.$field,$value);
                        else
                            $this->db->where($tbl.'.'.$field,$value);
                    }
                }
            }
        }
        
        return $this->db->count_all_results();
    }
    
    function get_user_filter_list($limit=0,$start=0,$filterdata='') {
        $this->db->from('users usr');
        $this->db->join('roles rls','usr.roleid = rls.roleid','left');
        
        if($filterdata!='') {
            $fitems = $filterdata['items'];
            foreach($fitems as $fitem) {
                $tbl = $fitem['table'];
                $field = $fitem['name'];
                $value = $fitem['value'];
                if( $value != '') {
                    if(is_array($value)) {
                        $this->db->where_in($tbl.'.'.$field,$value);
                    } else {
                        if($fitem['type']=='text')
                            $this->db->like($tbl.'.'.$field,$value);
                        else
                            $this->db->where($tbl.'.'.$field,$value);
                    }
                }
            }
        }
                
        $this->db->order_by("usr.userid", "asc");
        $this->db->limit($limit, $start); 
        $query = $this->db->get();
        //print_object($this->db->last_query());
        return $query->result_array();
    }
    
    function delete_user($data) {
        $this->db->where($data);
        $result = $this->db->delete('users');
        //print_object($result);exit;
        //print_object($this->db->last_query());exit;
        if ($this->db->affected_rows() > 0)
            return TRUE;
        return FALSE;
    }

}

?>